<?php 
require_once '../include.php';

$keywords=$_REQUEST['keywords']?$_REQUEST['keywords']:null;
$where=$keywords?"where concat_ws(' ',a.name,a.phone,a.email,a.username,b.prov_name) like '%{$keywords}%'":null;

$sql="select a.*,b.prov_name 
from public.user a 
join public.region b 
on a.area_code=b.prov_code
{$where}
";
//order by a.name desc,b.prov_code asc  limit {$pageSize}
// var_dump($sql);

$PG = new pgsql("gis25.jlu3s.com", "23456", "t01", "postgres", "GIScontest2025");  
$PG->connect(); 
if(!$PG){ 
	$db_error = "无法连接到PostGreSQL数据库！"; echo $db_error; 
}else{  
    $rows = $PG->fetchAll($sql);
	//var_dump($rows);
	//if(is_null($rows)){ echo "未查到相关数据";}else{ foreach($rows as $row):echo $row['prov_name'];endforeach;}
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>userList</title>

<style>

/* 操作栏*/
.caozuolan {
	display:flex;
	flex-direction: row;
	justify-content: flex-end;
	align-items: center;
	height:50px;
}
.search_user{
	/* background-color: #008000; */
	width:400px;

	display:flex;
	flex-direction: row;
	justify-content: flex-start;
	align-items: center;
}
.searchword{
	width:240px;
}
.btn_search_click{
	cursor:pointer;
	margin-left:20px;
}

.btn2 {
	padding: 2px 2px;color:white;
	background-color: #000; 
	margin: 0 5px;
	font-family: "微软雅黑"; 
	font-size: 12px;
	cursor:pointer;
	height:32px;
	border-radius:5px;
}

.add_user{
	/* background-color: #ff8040; */
	width:100px;
	margin-right:50px;
	margin-left:50px;
}

/*表格*/
.table{
	border-collapse: collapse; 
	width: 100%; 
	font-size: 12px;
}
.table th{
	background-color: #E8E8E8;
}
.table th,
.table td{
	border: solid 1px #ccc; 
	padding: 3px;
}
.label,.check{
	vertical-align: middle;
}
.check{
	margin-right: 4px;
}
.btn{
	padding: 2px 2px;
	color:white; 
	background-color: #008000; 
	margin: 0 5px;
	font-family: "微软雅黑"; 
	font-size: 12px;
	cursor:pointer; 
}
.btn:hover{
	background-color: #ff8040;
}
</style>
</head>

<body>
<h2>用户管理</h2>
<div class="details">
					<div class="caozuolan">
                        
                        <div class="search_user">                            
                            <div class="text">
                                <span>搜索：</span>
                                <input type="text" value="" class="searchword"  id="search" onkeypress="SearchUser()" placeholder="姓名、手机号、邮箱、用户名、家乡等" >
                            </div>
							<div class="btn_search_click"><input type="button" value="点击搜索" class="btn2" onclick="SearchUser()"></div>
                         </div>

						 <div class="add_user">
                            <input type="button" value="添&nbsp;加&nbsp;新&nbsp;用&nbsp;户"  class="btn2" onclick="AddUser()">
                        </div>
                    </div>
                    <!--表格-->
                    <table class="table" cellspacing="0" cellpadding="0">
                        <thead>
                            <tr >
                                <th>编号</th>
                                <th>姓名</th>
                                <th>性别</th>
                                <th>用户名</th>
                                <th>手机号</th>
                                <th>E-mail</th>
                                <th>家乡</th>
                                <th>角色</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
							<?php if(is_null($rows)){ echo "未查到相关数据";}else{ foreach($rows as $key=>$row):?>
                            <tr>
                                <!--各单元格循环出来-->								
                                <td><input type="checkbox" id="c" class="check" value=<?php echo $row['uid'];?>><label for="c1" class="label"><?php echo $key+1;?></label></td>
                                <td><?php echo $row['name'];?></td>
                                <!-- <td><?php echo $row['gender'];?></td> -->
								<td><?php if($row['gender']==1){echo "男";}elseif($row['gender']==2){echo "女";}else{echo "未知";} ?></td>
                                <td><?php echo $row['username'];?></td>
                                <td><?php echo $row['phone'];?></td>
                                <td><?php echo $row['email'];?></td>
                                <td><?php echo $row['prov_name'];?></td>
                                <!-- <td style="color:blue;"><?php echo $row['power_id'];?></td> -->
								<td style="color:blue;"><?php if($row['power_id']==1){echo "普通用户";}elseif($row['power_id']==0){echo "管理员";}elseif($row['power_id']==2){echo "<span style='color:gray;'>禁用/冻结状态</span>";}else{echo "未知";} ?></td>
                                <td align="center">                                    
                                    <input type="button" value="修改" class="btn" onclick="EditUser(<?php echo $row['uid'];?>)" style="cursor:pointer" />
									<input type="button" value="删除" class="btn" onclick="DeleteUser('<?php echo $row['uid'];?>','<?php echo $row['name'];?>')" style="cursor:pointer" />                                   
                                </td>								
                            </tr>
							<?php endforeach;}?>

                        </tbody>
                    </table>
                </div>
<script type="text/javascript">

    function EditUser(uid){
		window.location="EditUser.php?uid="+uid;
	}

	function DeleteUser(uid,name){
		if(window.confirm("您确认要删除【 " + name + " 】用户吗？")){
			window.location="doDeleteUser.php?uid="+uid;
		}
	}

	function SearchUser(){
		if(event.keyCode==13){
			var val=document.getElementById("search").value;
			window.location="userList.php?keywords="+val;
		}else{
			var val=document.getElementById("search").value;
			window.location="userList.php?keywords="+val;
		}
	}

	function AddUser(){
		window.location="AddUser.php";
	}

</script>
</body>
</html>